<?php
namespace Home\Controller;

use Think\Controller;

class ApiController extends Controller
{
    /**
     * 获取首页数据
     * @return [type] [description]
     */
    public function index()
    {
        // 注意 URL 一定要动态获取，不能 hardcode.
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $url      = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        //获取配置信息
        $config = getConfig($url);
        //传入配置信息到网页中
        $this->assign('config', $config);
        $this->display();
    }

    /**
     *  获取微信签名
     * @return [type] [description]
     */
    public function wechatSign()
    {
        //获取参数signature nonce token timestamp echostr
        $nonce     = I('get.nonce');
        $token     = 'g26';
        $timestamp = I('get.timestamp');
        $echostr   = I('get.echostr');       
        $signature = I('signature');
        //形成数组，按字典排序
        $arr = array($nonce, $timestamp, $token);
        sort($arr);
        //拼接成字符串，sha1加密，
        $str  = sha1(implode($arr));
        
        if ($str == $signature) {
            echo $echostr;
            exit;
        } else {
            echo 123;
        }
    }

    /**
     * 获取用户信息
     */
    public function userInfo()
    {

    }

    /**
     * 图片上传
     */

    public function upload()
    {
        try {
            $picture = $_FILES['picture'];
            if (!$picture['error'][0] == 4) {
                $upload           = new \Think\Upload(); // 实例化上传类
                $upload->maxSize  = 3145728; // 设置附件上传大小
                $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
                $upload->rootPath = './Public/Home/Uploads/'; // 设置附件上传目录
                $upload->saveName = array('uniqid', 'mypro_');
                $upload->autoSub  = true;
                $upload->subName  = array('date', 'Ymd');
                // 上传文件
                $info = $upload->upload();
                if (!$info) {
                    // 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {
                    // 上传成功 获取上传文件信息
                    foreach ($info as $file) {
                        $picture_list[] = $file['savepath'] . $file['savename'];
                    }
                    foreach ($picture_list as $vo) {
                        $condition[] = [
                            'picture' => $vo,
                            'user_id' => USER_ID,
                        ];
                    }
                    $photo = M('picture')
                        ->where($condition)
                        ->addAll();
                    if ($photo) {
                        success(['data' => $data]);
                    } else {
                        fail(101, '寫入失敗');
                    }
                }
            } else {
                fail(102, '未上傳任何圖片');
            }
        } catch (Exception $e) {
            fail(103, '服務器異常');
        }
    }

    /**
     * 用户余额充值
     */
    public function recharge()
    {

    }

    /**
     * 订单列表
     */
    public function orderList()
    {
        $user_id = I('post.user_id', '1');
        $num     = 10;
        $order   = M('order')
            ->where(['user_id' => $user_id])
            ->field('id,
                    avatar,
                    title,
                    time,
                    price,
                    status')
            ->limit($num)
            ->select();
        success(['data' => $order]);
    }

    /**
     * 获取优惠券
     */
    public function couponList()
    {
        $coupon = M('coupon')
            ->select();
        dump($coupon);exit(123);
    }

    /**
     *服务详情
     */
    public function serveDetail()
    {

    }

    /**
     * 优惠券详情
     */
    public function couponDetail()
    {
        $coupon = M('coupon');
        $list   = $coupon
            ->field('id,
                    title,
                    description,
                    avatar,
                    ensure,
                    shop_name,
                    date,
                    start_time,
                    end_time,
                    shop_detail')
            ->order('id')
            ->select();
        success(['data' => $list]);
    }

    /**
     * 商家，服务搜索
     */
    public function search()
    {
        $search = I('post.search');
    }

    /**
     * 首页轮播图
     */
    public function slideshow()
    {
        $slideshow = M('slideshow');
        $list      = $slideshow
            ->field('')
            ->order()
            ->select();
    }

}
